#include "reactor.hpp"
#include "socket.hpp"
#include "accepter.hpp"
#include <stdlib.h>

static void Usage(const std::string& proc)
{
    std::cout << "Usage: " << proc << " port" << std::endl;
}
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    //创建socket 监听
    int listen_sock = Sock::Socket();
    SetNonBlock(listen_sock);
    uint16_t port = (uint16_t)atoi(argv[1]);
    Sock::Bind(listen_sock, port);
    Sock::Listen(listen_sock);

    //创建Reactor对象
    Reactor* R = new Reactor();
    R->CreateEpoll();

    //将Reactor反应堆中加柴火
    Event* evl = new Event();
    evl->R = R;
    evl->sock = listen_sock;

    //链接管理器
    evl->RegisterEvent(Accepter, nullptr, nullptr);
    //将准备好的柴火放入反应堆Reactor中
    R->InsertEvent(evl, EPOLLIN | EPOLLET);
    
    //事件派发
    int timeout = 1000;
    for( ; ;)
    {
        R->Dispatcher(timeout);
        sleep(1);
    }

    return 0;
}